package com.changxianggu.cloud.crawler.core.utils;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Map;

/**
 * @author liuyuantao
 */
public class AliyunSmsUtil {

    private static final String ALIYUN_SMS_ACCESS_KEY = "LTAI4FjcYcA5JtKcQ3L5keFf";
    private static final String ALIYUN_SMS_ACCESS_SECRET = "qzMD0YXg99IiZueLNHrYGm1l7uKoLH";

    private static final String SEND_ACTION = "SendSms";
    private static final String SEND_BATCH_ACTION = "SendBatchSms";
    private static final String QUERY_DETAIL_ACTION = "QuerySendDetails";
    private static final String REGION_ID = "cn-hangzhou";
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";
    private static final String DEFAULT_PROFILE = "cn-hangzhou";
    private static final String VERSION = "2017-05-25";


    static {

    }

    /**
     * @param mobile        手机号
     * @param signName      签名
     * @param templateCode  验证码模板
     * @param templateParam 参数
     * @throws ClientException
     */
    public static String sendSms(String mobile, String signName, String templateCode,
                                 Map<String, String> templateParam) throws ClientException {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction(SEND_ACTION);
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumbers", mobile);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);

        request.putQueryParameter("TemplateParam", JSON.toJSONString(templateParam));
        try {
            CommonResponse response = clientInstance().getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 验证码类型短信,建议使用接口SendSms单独发送
     *
     * @param mobileJsonArray 接收短信的手机号码,JSON数组格式。
     * @param signJsonArray   短信签名名称,JSON数组格式,短信签名的个数必须与手机号码的个数相同、内容一一对应。
     * @param templateCode
     * @param paramJsonArray  短信模板变量对应的实际值,JSON格式。
     *                        且模板变量值的个数必须与手机号码、签名的个数相同、内容一一对应,
     *                        表示向指定手机号码中发对应签名的短信,且短信模板中的变量参数替换为对应的值。
     */
    public static String sendBatchSms(String mobileJsonArray, String signJsonArray,
                                      String templateCode, String paramJsonArray) {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction(SEND_BATCH_ACTION);
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumberJson", mobileJsonArray);
        request.putQueryParameter("SignNameJson", signJsonArray);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParamJson", paramJsonArray);
        try {
            CommonResponse response = clientInstance().getCommonResponse(request);
            return response.getData();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param mobile      接收短信的手机号码。
     * @param sendDate    短信发送日期，支持查询最近30天的记录。格式为yyyyMMdd，例如20181225。
     * @param pageSize    分页查看发送记录，指定每页显示的短信记录数量。取值范围为1~50。
     * @param currentPage 分页查看发送记录，指定发送记录的的当前页码。
     * @param bizId       发送回执ID，即发送流水号。调用发送接口SendSms或SendBatchSms发送短信时，返回值中的BizId字段。
     */
    public static String querySendDetails(String mobile, String sendDate, String pageSize, String currentPage, String bizId) {
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(DOMAIN);
        request.setSysVersion(VERSION);
        request.setSysAction(QUERY_DETAIL_ACTION);
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumber", mobile);
        request.putQueryParameter("SendDate", sendDate);
        request.putQueryParameter("PageSize", pageSize);
        request.putQueryParameter("CurrentPage", currentPage);
        request.putQueryParameter("BizId", bizId);
        try {
            CommonResponse response = clientInstance().getCommonResponse(request);
            return response.getData();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @return
     */
    public static IAcsClient clientInstance() {
        DefaultProfile profile = DefaultProfile.getProfile(DEFAULT_PROFILE,
                ALIYUN_SMS_ACCESS_KEY,
                ALIYUN_SMS_ACCESS_SECRET);
        return new DefaultAcsClient(profile);

    }
}
